package com.wrupple.muba.catalogs.client.module.services.logic.impl;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.wrupple.muba.catalogs.client.activity.CatalogActivity;
import com.wrupple.muba.catalogs.client.channels.CatalogReadingChannel;
import com.wrupple.muba.catalogs.client.module.services.logic.CatalogEntryAssembler;
import com.wrupple.muba.catalogs.client.module.services.logic.EntryRetrivingService;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.client.application.SingletonListDataCallbackWrapper;
import com.wrupple.muba.common.domain.FilterData;
import com.wrupple.muba.common.shared.StateTransition;
import com.wrupple.vegetate.client.util.JsArrayList;
import com.wrupple.vegetate.domain.CatalogDescriptor;
import com.wrupple.vegetate.domain.CatalogResponseContract;
import com.wrupple.vegetate.domain.JsVegetateResultSet;

public class EntryRetrivingServiceImpl extends AbstractCatalogService implements
		EntryRetrivingService {

	class BuildResponseFromSummary extends
			DataCallback<CatalogResponseContract> {

		private StateTransition<List<CatalogEntry>> callback;
		private String catalogid;

		public BuildResponseFromSummary(
				StateTransition<List<CatalogEntry>> wrapped, String catalogid) {
			this.callback = wrapped;
			this.catalogid = catalogid;
		}

		@Override
		public void execute() {
			if (result.getError() == null) {
				if (result.getResponse() == null) {
					GWT.log("Received empty response");
					callback.setResult(null);
				} else {

					JsVegetateResultSet resultSet = ((JavaScriptObject) result
							.getResponse().get(0)).cast();
					JsArray<CatalogEntry> list = assemblyService.processResultSet(resultSet,
							catalogid);
					
					List<CatalogEntry> regreso= JsArrayList.arrayAsList(list);
					callback.setResult(regreso);
				}

				callback.execute();
			} else {
				GWT.log(result.getError());
			}
		}

	}

	private Provider<CatalogReadingChannel> serviceProvider;
	private CatalogEntryAssembler assemblyService;

	@Inject
	public EntryRetrivingServiceImpl(Provider<CatalogReadingChannel> service,CatalogEntryAssembler assemblyService) {
		super();
		this.serviceProvider = service;
		this.assemblyService=assemblyService;
	}

	

	@Override
	public void retriveFilteredEntries(String catalogid, FilterData filter,
			StateTransition<List<? extends JavaScriptObject>> callback,
			EventBus bus) {
		StateTransition hook = new EntryRetrivingServiceHook.List(catalogid,
				bus);
		hook.hook(callback);

		CatalogReadingChannel service = serviceProvider.get();
		StateTransition<CatalogResponseContract> vegetateCallback = createChannelCallback(
				hook, catalogid, service);
		service.send(filter, vegetateCallback, catalogid,
				CatalogActivity.READ_ACTION);
	}

	@Override
	public void retriveFilteredEntries(CatalogDescriptor catalog,
			FilterData filter, StateTransition<List<CatalogEntry>> callback,
			EventBus bus) {
		StateTransition dumb = callback;
		retriveFilteredEntries(catalog.getId(), filter, dumb, bus);
	}

	@Override
	public void retriveFilteredSummaryEntries(String catalogid,
			FilterData filter,
			StateTransition<List<? extends JavaScriptObject>> callback,
			EventBus eventBus) {
		StateTransition hook = callback;

		CatalogReadingChannel service = serviceProvider.get();
		catalogid = super.tokenizeCatalogId(catalogid, service);
		StateTransition<CatalogResponseContract> vegetateCallback = new BuildResponseFromSummary(
				hook, catalogid);
		service.send(filter, vegetateCallback, catalogid,
				CatalogActivity.SUMMARY_ACTION);

	}

	@Override
	public void retriveEntryById(String entryId, String catalogid,
			StateTransition<? extends JavaScriptObject> callback, EventBus bus) {
		callback.hook((StateTransition) new EntryRetrivingServiceHook(
				catalogid, bus));

		CatalogReadingChannel service = serviceProvider.get();
		StateTransition dumb = callback;
		StateTransition<List<CatalogEntry>> singletonSelector = new SingletonListDataCallbackWrapper<CatalogEntry>(
				dumb);
		StateTransition<CatalogResponseContract> vegetateCallback = createChannelCallback(
				singletonSelector, catalogid, service);

		service.send(null, vegetateCallback, catalogid,
				CatalogActivity.READ_ACTION, entryId);
	}



}
